--[[----------------------------------------------------------------------------
LR2Util_IM.lua
--------------------------------------------------------------------------------
Copyright 2010 Andriy Doroshchuk
 All Rights Reserved.
------------------------------------------------------------------------------]]

local LrPathUtils = import 'LrPathUtils'
local LrTasks	= import 'LrTasks'

--============================================================================--
lbracket 	= '\\('
rbracket	= '\\)'
percent	= '%'
if WIN_ENV == true then
	lbracket 	= '('
	rbracket	= ')'
	percent	= '%%'
end

makeColor = function (color) return '"rgb(' .. color:red()*255 .. ',' .. color:green()*255 .. ',' .. color:blue()*255 .. ')"' end
makeWidth = function (width) return width .. 'x' .. width end

GetTool = function (path2IM)
	local toolPath
	if path2IM and string.len(path2IM) then 
		if WIN_ENV == true then
			toolPath = LrPathUtils.child(path2IM, 'convert.exe')
		else
			toolPath = LrPathUtils.child(path2IM, 'convert')
		end
	end
	local doPostProcess = toolPath and string.len(toolPath)
	return toolPath
end

RunIM = function (toolPath, path, command, rendition)
	local commandline = '"' .. toolPath .. '" "' .. path .. '" ' .. command .. ' "' .. path .. '"'
	if WIN_ENV == true then 
		commandline = '"' .. commandline .. '"'
	end
	--LOG (commandline)
	if LrTasks.execute(commandline) ~= 0 then
		rendition:renditionIsDone( false, "Failed to render frame")
	else
		rendition:renditionIsDone( true )
	end
end